import Vue from 'vue'
import VueRouter from 'vue-router'
import store from './../store'
import { Message } from 'element-ui'
import cache from '@u/cache'
import { PERMISSIONS } from '@/common/user'

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch((err) => err)
}
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@v/layout'),
    // redirect: '/one-map',
    children: [
      {
        path: '/home',
        name: 'index',
        component: () => import('@v/home-view'),
        meta: {
          title: '首页',
          icon: '首页',
          role: 3,
          roleId: ['citymanager', 'checkmanager', 'streetmanager'],
          permission: PERMISSIONS.首页,
        },
      },
      // {
      //   path: "/indicator-manage",
      //   name: "indicatorManage",
      //   component: () => import("@v/indicator-manage"),
      //   meta: { title: "指标体系管理", icon: "指标体系管理", role: 3 },
      //   redirect: "indicator-manage/main",
      //   children: [
      //     {
      //       path: "main",
      //       name: "main",
      //       component: () => import("@v/indicator-manage/main"),
      //     },
      //     {
      //       path: "addTask",
      //       name: "addTask",
      //       component: () => import("@v/indicator-manage/addTask"),
      //     },
      //     {
      //       path: "details/:id",
      //       name: "details",
      //       component: () => import("@v/indicator-manage/details"),
      //     },
      //     {
      //       path: "edit/:id",
      //       name: "edit",
      //       component: () => import("@v/indicator-manage/edit"),
      //     },
      //   ],
      // },
      {
        path: '/one-map',
        name: 'oneMap',
        component: () => import('@v/one-map/index.vue'),
        meta: {
          title: '体检调查信息管理',
          icon: '体检数据汇聚',
          permission: PERMISSIONS.体检调查信息管理,
        },
        children: [],
      },
      {
        path: '/data-converge',
        name: 'dataConverge',
        component: () => import('@v/data-converge'),
        meta: {
          title: '指标数据管理',
          icon: '指标体系管理',
          permission: PERMISSIONS.指标数据管理,
        },
        // redirect: '/data-converge/index',
        children: [
          {
            path: 'indicator-manage',
            name: 'indicatorManage',
            component: () => import('@v/data-converge/indicator-manage/main'),
            meta: {
              permission: PERMISSIONS.基础指标,
            },
          },
          {
            path: 'indicator-manage-addTask',
            name: 'indicatorManageAddTask',
            component: () =>
              import('@v/data-converge/indicator-manage/add/Add.vue'),
            meta: {
              permission: PERMISSIONS.基础指标,
            },
          },
          {
            path: 'indicator-manage-details/:id',
            name: 'indicatorManageDetails',
            component: () =>
              import('@v/data-converge/indicator-manage/add/Add.vue'),
            meta: {
              permission: PERMISSIONS.基础指标,
            },
          },
          {
            path: 'indicator-manage-edit/:id',
            name: 'indicatorManageEdit',
            component: () =>
              import('@v/data-converge/indicator-manage/add/Add.vue'),
            meta: {
              permission: PERMISSIONS.基础指标,
            },
          },
          {
            path: 'indicator-manage-special',
            name: 'indicatorManageSpecial',
            component: () =>
              import('@v/data-converge/indicator-manage-special/main'),
            meta: {
              permission: PERMISSIONS.特色指标,
            },
          },
          {
            path: 'indicator-manage-special-addTask',
            name: 'indicatorManageSpecialAddTask',
            component: () =>
              import('@v/data-converge/indicator-manage-special/add/Add.vue'),
            meta: {
              permission: PERMISSIONS.特色指标,
            },
          },
          {
            path: 'indicator-manage-special-details/:id',
            name: 'indicatorManageSpecialDetails',
            component: () =>
              import('@v/data-converge/indicator-manage-special/add/Add.vue'),
            meta: {
              permission: PERMISSIONS.特色指标,
            },
          },
          {
            path: 'indicator-manage-special-edit/:id',
            name: 'indicatorManageSpecialEdit',
            component: () =>
              import('@v/data-converge/indicator-manage-special/add/Add.vue'),
            meta: {
              permission: PERMISSIONS.特色指标,
            },
          },
          {
            path: 'taskPublish',
            name: 'taskPublish',
            component: () => import('@v/data-converge/publish'),
            meta: {
              permission: PERMISSIONS.任务管理,
            },
          },
          // {
          //   path: "taskEdit",
          //   name: "taskEdit",
          //   component: () => import("@v/data-converge/publish/edit"),
          //   props: true,
          // },
          {
            path: 'fillInList',
            name: 'fillInList',
            component: () => import('@v/data-converge/fill-in'),
            meta: {
              permission: PERMISSIONS.数据填报,
            },
          },
          {
            path: 'fillInEdit',
            name: 'fillInEdit',
            component: () => import('@v/data-converge/fill-in/edit'),
            meta: {
              permission: PERMISSIONS.数据填报,
            },
          },
          {
            path: 'dataRelease',
            name: 'dataRelease',
            component: () => import('@v/data-converge/data-release'),
            // meta: { title: "数据发布" },
            meta: {
              permission: PERMISSIONS.报告生成,
            },
          },
          {
            path: 'dataReleaseEdit',
            name: 'dataReleaseEdit',
            component: () => import('@v/data-converge/data-release/edit'),
            // meta: { title: "数据发布编辑" },
            meta: {
              permission: PERMISSIONS.报告生成,
            },
          },
          {
            path: 'dataReport',
            name: 'dataReport',
            component: () => import('@v/data-converge/report'),
            // meta: { title: "数据发布" },
            meta: {
              permission: PERMISSIONS.报告上传,
            },
          },
          // {
          //   path: "dataSearchDep",
          //   name: "dataSearchDep",
          //   component: () => import("@v/data-converge/data-search/dept"),
          // },
          // {
          //   path: "dataSearchIndex",
          //   name: "dataSearchIndex",
          //   component: () => import("@v/data-converge/data-search/index"),
          // },
          // {
          //   path: "dataSearchDetail",
          //   name: "dataSearchDetail",
          //   component: () => import("@v/data-converge/data-search/detail"),
          // },
          // {
          //   path: "progressManage",
          //   name: "progressManage",
          //   component: () => import("@v/data-converge/progress-manage"),
          //   meta: {
          //     keepAlive: true, // 需要被缓存
          //   },
          // },
          // {
          //   path: "progressManageEdit",
          //   name: "progressManageEdit",
          //   component: () => import("@v/data-converge/progress-manage/edit"),
          // },
        ],
      },
      // {
      //   path: "/medical-data-release",
      //   name: "medicalDataRelease",
      //   component: () => import("@v/medical-data-release"),
      //   meta: { title: "体检数据发布", icon: "体检数据发布", role: 3 },
      //   redirect: "medical-data-release/analysis",
      //   children: [
      //     {
      //       path: "dataRelease",
      //       name: "dataRelease",
      //       component: () => import("@v/medical-data-release/data-release"),
      //       meta: { title: "数据发布" },
      //     },
      //     {
      //       path: "dataEdit",
      //       name: "dataEdit",
      //       component: () =>
      //         import("@v/medical-data-release/data-release/edit"),
      //       meta: { title: "数据发布编辑" },
      //     },
      //     {
      //       path: "analysis",
      //       name: "analysis",
      //       component: () => import("@v/medical-data-release/analysis"),
      //       meta: { title: "分析视角" },
      //     },
      //     {
      //       path: "analysisEdit",
      //       name: "analysisEdit",
      //       component: () => import("@v/medical-data-release/analysis/edit"),
      //       meta: { title: "分析视角编辑" },
      //     },
      //   ],
      // },
      {
        path: '/city-medical',
        name: 'cityMedical',
        // component: () => import("@v/city-medical"),
        meta: {
          title: '成果展示',
          icon: '体检数据查询',
          role: 3,
          roleId: 'citymanager',
          permission: PERMISSIONS.成果展示,
        },
      },
      // {
      //   path: "/city-view",
      //   name: "cityView",
      //   component: () => import("@v/city-view"),
      //   meta: { title: "城市更新", icon: "城市更新" },
      // },
      // {
      //   path: '/special-evaluate',
      //   name: 'specialEvaluate',
      //   component: () => import('@v/special-evaluate'),
      //   meta: { title: '专项评估', icon: '专项评估' },
      // },
      // {
      //   path: '/social-survey',
      //   name: 'socialSurvey',
      //   component: () => import('@v/social-survey'),
      //   meta: { title: '社会调查', icon: '社会调查' },
      // },

      {
        path: '/system-manage',
        name: 'systemManage',
        component: () => import('@v/system-manage'),
        redirect: 'system-manage/user',
        meta: {
          title: '系统管理',
          icon: '系统管理',
          role: 3,
          roleId: 'citymanager',
        },
        children: [
          {
            path: 'user',
            name: 'user',
            component: () => import('@v/system-manage/user'),
          },
          {
            path: 'department',
            name: 'department',
            component: () => import('@v/system-manage/department'),
          },
          {
            path: 'area',
            name: 'area',
            component: () => import('@v/system-manage/area'),
          },
          {
            path: 'auth',
            name: 'auth',
            component: () => import('@v/system-manage/auth'),
          },
          {
            path: 'basemap',
            name: 'basemap',
            component: () => import('@v/system-manage/basemap'),
          },
          {
            path: 'log',
            name: 'log',
            component: () => import('@v/system-manage/log'),
          },
        ],
      },
      {
        path: '/backend-management',
        name: 'BackendManagement',
        component: () =>
          import('@/views/backend-management/BackendManagement.vue'),
        redirect: 'backend-management/user-management',
        meta: {
          title: '运维管理',
          icon: '系统管理',
          role: 3,
          roleId: 'citymanager',
          permission: PERMISSIONS.后台管理,
        },
        children: [
          {
            path: 'user-management',
            name: 'user-management',
            component: () =>
              import('@/views/backend-management/components/user/index.vue'),
            meta: {
              permission: PERMISSIONS.用户管理,
            },
          },
          {
            path: 'session-management',
            name: 'session-management',
            component: () =>
              import(
                '@/views/backend-management/components/session-management/index.vue'
              ),
            meta: {
              permission: PERMISSIONS.会话管理,
            },
          },
          {
            path: 'blacklist-management',
            name: 'blacklist-management',
            component: () =>
              import(
                '@/views/backend-management/components/blacklist/index.vue'
              ),
            meta: {
              permission: PERMISSIONS.黑名单管理,
            },
          },
          {
            path: 'role-management',
            name: 'role-management',
            component: () =>
              import('@/views/backend-management/components/role/index.vue'),
            meta: {
              permission: PERMISSIONS.角色管理,
            },
          },
          {
            path: 'dept-management',
            name: 'dept-management',
            component: () =>
              import('@/views/backend-management/components/dept/index.vue'),
            meta: {
              permission: PERMISSIONS.部门管理,
            },
          },
          {
            path: 'menu-management',
            name: 'menu-management',
            component: () =>
              import('@/views/backend-management/components/menu/index.vue'),
            meta: {
              permission: PERMISSIONS.菜单管理,
            },
          },
          {
            path: 'login-log-management',
            name: 'login-log-management',
            component: () =>
              import(
                '@/views/backend-management/components/login-log/index.vue'
              ),
            meta: {
              permission: PERMISSIONS.登录日志管理,
            },
          },
          {
            path: 'action-log-management',
            name: 'action-log-management',
            component: () =>
              import(
                '@/views/backend-management/components/action-log/index.vue'
              ),
            meta: {
              permission: PERMISSIONS.操作日志记录,
            },
          },
          {
            path: 'base-map-resource',
            name: 'map-resource',
            component: () =>
              import(
                '@/views/backend-management/components/base-map-resource/index.vue'
              ),
            meta: {
              permission: PERMISSIONS.底图资源管理,
            },
          },
          {
            path: 'data-catalog',
            name: 'data-catalog',
            component: () =>
              import(
                '@/views/backend-management/components/data-catalog/index.vue'
              ),
            meta: {
              permission: PERMISSIONS.数据目录管理,
            },
          },
        ],
      },
      {
        path: '/project-update',
        name: 'projectUpdate',
        component: () => import('@v/project-update'),
        meta: {
          title: '更新项目管理',
          icon: '更新项目管理',
          permission: PERMISSIONS.更新项目管理,
        },
        // redirect: '/data-converge/index',
        children: [
          {
            path: 'area-storage',
            name: 'areaStorage',
            component: () =>
              import('@v/project-update/inventory-review/area-storage'),
            meta: {
              permission: PERMISSIONS.片区入库,
            },
          },
          {
            path: 'area-storage-detail',
            name: 'areaStorageDetail',
            component: () =>
              import('@v/project-update/inventory-review/area-storage/detail'),
            meta: {
              permission: PERMISSIONS.片区入库,
            },
          },
          {
            path: 'project-inventory',
            name: 'projectInventory',
            component: () =>
              import('@v/project-update/inventory-review/project-inventory'),
            meta: {
              permission: PERMISSIONS.项目入库,
            },
          },
          {
            path: 'project-inventory-detail',
            name: 'projectInventoryDetail',
            component: () =>
              import(
                '@v/project-update/inventory-review/project-inventory/detail'
              ),
            meta: {
              permission: PERMISSIONS.项目入库,
            },
          },
          {
            path: 'sub-project-inventory',
            name: 'subProjectInventory',
            component: () =>
              import(
                '@v/project-update/inventory-review/sub-project-inventory'
              ),
            meta: {
              permission: PERMISSIONS.子项目入库,
            },
          },
          {
            path: 'library-manage',
            name: 'libraryManage',
            component: () => import('@v/project-update/library-manage'),
            meta: {
              permission: PERMISSIONS.项目库管理,
            },
          },
          {
            path: 'library-manage-detail',
            name: 'libraryManageDetail',
            component: () => import('@v/project-update/library-manage/detail'),
            meta: {
              permission: PERMISSIONS.项目库管理,
            },
          },
          {
            path: 'library-manage-edit',
            name: 'libraryManageEdit',
            component: () =>
              import('@v/project-update/library-manage/edit.vue'),
            meta: {
              permission: PERMISSIONS.项目库管理,
            },
          },
          {
            path: 'annual-plan',
            name: 'annualPlan',
            component: () => import('@v/project-update/annual-plan'),
            meta: {
              permission: PERMISSIONS.年度计划,
            },
          },
          {
            path: 'annual-plan-detail',
            name: 'annualPlanDetail',
            component: () => import('@v/project-update/annual-plan/detail.vue'),
            meta: {
              permission: PERMISSIONS.年度计划,
            },
          },
          {
            path: 'month-report',
            name: 'monthReport',
            component: () => import('@v/project-update/month-report'),
            meta: {
              permission: PERMISSIONS.项目月报,
            },
          },
          {
            path: 'month-report-detail',
            name: 'monthReportDetail',
            component: () =>
              import('@v/project-update/month-report/detail.vue'),
            meta: {
              permission: PERMISSIONS.项目月报,
            },
          },
          {
            path: 'general-ledger',
            name: 'generalLedger',
            component: () =>
              import('@v/project-update/project-ledger/general-ledger'),
            meta: {
              permission: PERMISSIONS.总台账,
            },
          },
          {
            path: 'general-ledger-detail',
            name: 'generalLedgerDetail',
            component: () =>
              import('@v/project-update/project-ledger/general-ledger/detail'),
            meta: {
              permission: PERMISSIONS.总台账,
            },
          },
          {
            path: 'unified-ledger',
            name: 'unifiedLedger',
            component: () =>
              import('@v/project-update/project-ledger/unified-ledger'),
            meta: {
              permission: PERMISSIONS.入统台账,
            },
          },
          {
            path: 'unified-ledger-detail',
            name: 'unifiedLedgerDetail',
            component: () =>
              import('@v/project-update/project-ledger/unified-ledger/detail'),
            meta: {
              permission: PERMISSIONS.入统台账,
            },
          },
          {
            path: 'financial-ledger',
            name: 'financialLedger',
            component: () =>
              import('@v/project-update/project-ledger/financial-ledger'),
            meta: {
              permission: PERMISSIONS.金融台账,
            },
          },
          {
            path: 'financial-ledger-detail',
            name: 'financialLedgerDetail',
            component: () =>
              import(
                '@v/project-update/project-ledger/financial-ledger/detail'
              ),
            meta: {
              permission: PERMISSIONS.金融台账,
            },
          },
        ],
      },
      {
        path: '/screen-management',
        name: 'screenManagement',
        component: () => import('@v/screen-management/ScreenManagement.vue'),
        redirect: 'screen-management/screen-config',
        meta: {
          title: '大屏管理',
          icon: '大屏管理',
          permission: PERMISSIONS.大屏管理,
        },
        children: [
          {
            path: 'indicator-management',
            name: 'indicator-management',
            component: () => import('@v/screen-management/indicator-management/index.vue'),
            meta: {
              permission: PERMISSIONS.指标管理,
            },
          },
          {
            path: 'screen-config',
            name: 'screen-config',
            component: () => import('@v/screen-management/screen-config/index.vue'),
            meta: {
              permission: PERMISSIONS.大屏配置,
            },
          },
          {
            path: 'indicator-config',
            name: 'indicator-config',
            component: () => import('@v/screen-management/indicator-config/index.vue'),
            meta: {
              permission: PERMISSIONS.指标配置,
            },
          },
          {
            path: 'report-platform',
            name: 'reportPlatform',
            component: () => import('@v/screen-management/report-platform/layout.vue'),
            redirect: 'screen-management/report-platform/project-management',
            meta: {
              permission: PERMISSIONS.汇报平台,
            },
            children: [
              {
                path: 'project-management',
                name: 'project-management',
                component: () => import('@v/screen-management/report-platform/project-management/index.vue'),
                meta: {
                  permission: PERMISSIONS.项目管理,
                },
              },
              {
                path: 'template-management',
                name: 'template-management',
                component: () => import('@v/screen-management/report-platform/template-management/index.vue'),
                meta: {
                  permission: PERMISSIONS.模板管理,
                },
              },
            ]
          },
        ],
      },
    ],
  },
  {
    path: '/markdown-preview',
    name: 'MarkdownPreview',
    component: () => import('@v/markdown-preview'),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@v/login'),
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

// const staticRouter = ['/login']
// /* 路由守卫 */
// // 切换路由之前
// router.beforeEach(({ meta, path }, from, next) => {
//   // console.log(meta, path, from)
//   const isLogin = Boolean(store.state.app.user)
//   if (!isLogin && !staticRouter.includes(path)) return next({ path: '/login' })
//   if (isLogin && staticRouter.includes(path)) return next({ path: '/' })
//   next()
// })

// 切换路由完成
router.afterEach((route) => {
  // console.log(route)
})

const allowAnonymityPaths = ['/login']

router.beforeEach((to, from, next) => {
  const currentUserStore = store.state.user
  const token = store.state.app.token
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      // if (!allowAnonymityPaths.includes(to.path) && to.meta.permission) {
      //   if (
      //     currentUserStore.currentUser &&
      //     !currentUserStore.currentUser.hasPermission(to.meta.permission + '')
      //   ) {
      //     Message.error('该账号没有页面权限')
      //     next(false)
      //   } else {
      //     next()
      //   }
      // } else {
      //   next()
      // }
      next()
      // next('/home')
    } else {
      next('/login')
    }
  }
})

export default router
